package demo.turkey;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import demo.util.JsonReadUtils;
import groovy.text.GStringTemplateEngine;
import groovy.text.Template;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

/**
 * @author lingyun.wang on @date 2022/1/17
 */
public class generateSql {
    private static String jsonTemplate = "INSERT INTO cloud_meta_data_info (id,data_source,data_path,field_code,field_name,data_type,check_value,create_time) VALUES (nextval('cloud_meta_data_info_seq'),'$dataSource','$dataPath','$fieldCode','$fieldName','$dataType','1','2022-01-17 15:00:00.000');";
    private static String updateSqlTemplate = "update cloud_meta_data_info set meta_data_desc = \"$fullName\" where id = $id;";
    private static GStringTemplateEngine engine = new GStringTemplateEngine();
    private static Template t;
    private static Template t2;
    private static Map<String,Object> nameMap = JsonReadUtils.readObject("turkey/name.json");
    private static Map<String,Object> typeMap = JsonReadUtils.readObject("turkey/type.json");
    static {
        try {
            t = engine.createTemplate(jsonTemplate);
            t2 = engine.createTemplate(updateSqlTemplate);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test(){
        JSONObject nameList = JsonReadUtils.readObject("turkey/param.json");
        iteration(nameList,"");
    }
    public void iteration(JSONObject json ,String parentPath){
        for (Map.Entry<String, Object> entry1 : json.entrySet()) {
            JSONObject next = getJSONObject(json,entry1.getKey());

            if(next != null){
                iteration(next,buildDataPath(parentPath,entry1.getKey()));
            }else {
                printSql(parentPath,entry1.getKey());
            }
        }
    }

    private JSONObject getJSONObject(JSONObject json, String key) {
        try {
            return json.getJSONArray(key).getJSONObject(0);
        }catch (Exception e){

        }
        try {
            return json.getJSONObject(key);
        }catch (Exception e){

        }
        return null;
    }

    @Test
    public void test2() {
        JSONObject configList = JsonReadUtils.readObject("turkey/config.json");
        for (Map.Entry<String, Object> entry : configList.entrySet()) {
            Map<String,String> map = makeMap(entry.getKey(),entry.getKey(),entry.getValue().toString(),"String");
            System.out.println(t.make(map));
        }
    }

    @Test
    public void test3() {
        JSONArray configList = JsonReadUtils.readArray("turkey/temp.json");
        for (int i = 0; i < configList.size(); i++) {
            JSONObject object = configList.getJSONObject(i);
            String dataPath = object.getString("data_path");
            int id = object.getInteger("id");
            Map<String,Object> map = Maps.newHashMap();
            map.put("fullName",buildFullName(dataPath));
            map.put("id",id);
            System.out.println(t2.make(map));
        }
    }

    private String buildFullName(String dataPath) {
        String[] path = dataPath.split("\\.");
        StringBuffer sb = new StringBuffer();
        for (String fieldCode : path) {
            sb.append("."+toString(nameMap.get(fieldCode)));
        }
        sb.deleteCharAt(0);
        return sb.toString();
    }


    public String buildDataPath(String parentPath,String fieldCode){
        return "".equals(parentPath)  ? fieldCode : parentPath+"."+fieldCode;
    }
    public void printSql(String parentPath,String fieldCode){
        Map<String,String> map = makeMap(buildDataPath(parentPath,fieldCode),fieldCode,toString(nameMap.get(fieldCode)),toString(typeMap.get(fieldCode)));
        System.out.println(t.make(map));
    }

    private String toString(Object o) {
        if(o == null){
            return "";
        }
        return o.toString();
    }

    public Map<String,String> makeMap(String dataPath,String fieldCode,String fieldName,String dataType){
        Map<String,String> map = Maps.newHashMap();
        map.put("dataSource","config");
        map.put("dataPath",dataPath);
        map.put("fieldCode",fieldCode);
        map.put("fieldName",fieldName);
        map.put("dataType",dataType);
        return map;
    }
}
